<?php

declare(strict_types=1);

namespace App\Admin\Controller\System\Monitor;


use App\Admin\Annotation\Auth;
use App\Admin\Annotation\Permission;
use App\Admin\Service\System\CacheMonitorService;
use DI\Attribute\Inject;
use Newbie\Annotation\Attributes\Route\Controller;
use Newbie\Annotation\Attributes\Route\DeleteMapping;
use Newbie\Annotation\Attributes\Route\GetMapping;
use Newbie\Annotation\Attributes\Route\PostMapping;
use Newbie\NewbieController;
use support\Log;
use support\Response;
use Webman\Route;

/**
 * 缓存监控
 * Class CacheMonitorController
 * @package App\System\Controller\Monitor
 */
#[Controller(prefix: "system/cache"), Auth]
class CacheMonitorController extends NewbieController {
    #[Inject]
    protected CacheMonitorService $service;

    /**
     * 获取Redis服务器信息
     */
    #[GetMapping("monitor"), Permission("system:cache:monitor")]
    public function getCacheInfo (): Response {
        return $this->success($this->service->getCacheServerInfo());
    }

    /**
     * 查看key内容
     */
    #[PostMapping("view")]
    public function view (): Response {
        Log::notice(var_export(Route::getRoutes(), true));
        return $this->success(['content' => $this->service->view(request()->input('key'))]);
    }

    /**
     * 删除一个缓存
     */
    #[DeleteMapping("delete"), Permission("system:cache:delete")]
    public function delete (): Response {
        return $this->service->delete(request()->input('key', ''))
            ? $this->success()
            : $this->error();
    }

    /**
     * 清空所有缓存
     */
    #[DeleteMapping("clear"), Permission("system:cache:clear")]
    public function clear (): Response {
        return $this->service->clear() ? $this->success() : $this->error();
    }
}